<?php

namespace admin\models;

use Yii;

/**
 * This is the model class for table "mm_user_info".
 *
 * @property integer $mi_id
 * @property integer $mo_id
 * @property string $mi_name
 * @property string $mi_sex
 * @property integer $mi_age
 * @property integer $mi_height
 * @property string $mi_weight
 * @property string $mi_birth
 * @property string $mi_zodiac
 * @property string $mi_root
 * @property string $mi_edu
 * @property string $mi_sight
 * @property string $mi_cardid
 * @property integer $mi_status
 * @property string $created_time
 * @property string $updated_time
 *
 * @property Comment[] Comments
 * @property Detail Details
 * @property Experience[] Experiences
 * @property Extra Extras
 * @property Family[] Families
 * @property Health Healths
 * @property Ill[] Ills
 * @property Meeting[] Meetings
 * @property Meeting[] Meetings0
 * @property Requirements Requirements
 * @property Photos Photos
 */
class UserInfo extends \yii\db\ActiveRecord
{


    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'mm_user_info';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['mi_height','mi_name','mi_sex','mi_weight','mi_birth','mi_zodiac','mi_edu','mi_sight','mi_cardid'],'required','on'=>['add','update']],
            [['mo_id', 'mi_height', 'mi_status','mi_age'], 'integer'],
            [['mi_sex', 'mi_zodiac', 'mi_edu', 'mi_sight'], 'string'],
            [['mi_weight'], 'number'],
            [['created_time', 'updated_time'], 'safe'],
            [['mi_name'], 'string', 'max' => 11],
            [['mi_birth'], 'string', 'max' =>10],
            [['mi_root'], 'string', 'max' => 100],
            [['mi_cardid'], 'string', 'max' => 18],
            [['mi_cardid'],'unique','targetClass' => '\admin\models\UserInfo','on'=>'add','message'=>'该身份证号用户已存在'],
            [['mi_cardid'],'checkCardid','on'=>['update','add']],
            [['mi_cardid'],'checkThisCardid','on'=>'update'],
        ];
    }
    public function scenarios()
    {
        $scenarios = parent::scenarios();
        $scenarios['add']=['mi_name','mi_sex','mi_height','mi_weight','mi_birth','mi_zodiac','mi_root','mi_edu','mi_sight','mi_zodiac','mi_cardid','mo_id'];
        $scenarios['update']=['mi_name','mi_sex','mi_height','mi_weight','mi_birth','mi_zodiac','mi_root','mi_edu','mi_sight','mi_zodiac','mi_cardid','mo_id'];
        return $scenarios;
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'mi_id' => '用户基本信息id',
            'mo_id' => '添加管理员id',
            'mi_name' => '姓名',
            'mi_sex' => '性别',
            'mi_age' => '年龄',
            'mi_height' => '身高',
            'mi_weight' => '体重',
            'mi_birth' => '出生年月',
            'mi_zodiac' => '属相',
            'mi_root' => '籍贯',
            'mi_edu' => '学历',
            'mi_sight' => '视力状态',
            'mi_cardid' => '身份证号',
            'mi_status' => '状态',
            'created_time' => 'Created Time',
            'updated_time' => 'Updated Time',
        ];
    }


    public function checkCardid(){
        if(!preg_match("/^[0-9]{18}$/",$this->mi_cardid)) {
            $this->addError('mi_cardid', '请输入18位身份证号');
        }
    }
    public function checkThisCardid(){
        $user = static::find()->where(['and',"mi_cardid='".$this->mi_cardid."'",'mi_id<>'.$this->mi_id])->one();
        if(!empty($user)){
            $this->addError('mi_cardid','该身份证用户已存在');
        }
    }
    /**
     * @return \yii\db\ActiveQuery
     */
    public function getComments()
    {
        return $this->hasMany(UserComment::className(), ['mu_id' => 'mi_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getDetails()
    {
        return $this->hasOne(UserDetail::className(), ['mu_id' => 'mi_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getExperiences()
    {
        return $this->hasMany(UserExperience::className(), ['mu_id' => 'mi_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getExtras()
    {
        return $this->hasOne(UserExtra::className(), ['mu_id' => 'mi_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getFamilies()
    {
        return $this->hasMany(UserFamily::className(), ['mu_id' => 'mi_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getHealths()
    {
        return $this->hasOne(UserHealth::className(), ['mu_id' => 'mi_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getIlls()
    {
        return $this->hasMany(UserIll::className(), ['mu_id' => 'mi_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getMeetings()
    {
        return $this->hasMany(UserMeeting::className(), ['mu_id' => 'mi_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getMeetings0()
    {
        return $this->hasMany(UserMeeting::className(), ['mu_targrt' => 'mi_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getRequirements()
    {
        return $this->hasOne(UserRequirements::className(), ['mu_id' => 'mi_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getPhotos()
    {
        return $this->hasOne(UserPhoto::className(), ['mu_id' => 'mi_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getOwner()
    {
        return $this->hasOne(Admin::className(), ['ma_id' => 'mo_id']);
    }
}
